package lv.imho.client.view;

import java.util.ArrayList;
import java.util.List;

import lv.imho.client.model.DataSourceTransferObject;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Represents list of data sources.
 * 
 * @author Igors Gulbinskis
 */
public class DataSourceListPanel extends VerticalPanel {
	
	private final String WIDTH = "100%";
	
	private List<DataSourceTransferObject> dataSources;
	private List<DataSourceComponent>      dataSourceComponents;
	
	public DataSourceListPanel(List<DataSourceTransferObject> dataSources) {
		super();
		
		this.dataSources = dataSources;
		this.dataSourceComponents = new ArrayList<DataSourceComponent>();
		setWidth(WIDTH);
		
		initChildComponents();
	}
	
	private void initChildComponents() {
		Label title = new Label("Datu avoti <" + dataSources.size() + ">");
		title.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		DOM.setStyleAttribute(title.getElement(), "background", "#8cabdf");
		DOM.setStyleAttribute(title.getElement(), "fontWeight", "bold");
		add(title);
		
		DataSourceComponent dataSourceComponent;
		if (dataSources != null && dataSources.size() > 0) {
			for (DataSourceTransferObject dataSource : dataSources) {
				dataSourceComponent = new DataSourceComponent(dataSource);
				dataSourceComponents.add(dataSourceComponent);
				add(dataSourceComponent);
			}
		}
	}
	
	public List<DataSourceComponent> getDataSourceComponents() {
		return dataSourceComponents;
	}
	
}
